package com.lpwa.quartz.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lpwa.common.core.text.Convert;
import com.lpwa.common.utils.RedisUtil;
import com.lpwa.common.utils.StaticRedisUtil;
import com.lpwa.common.utils.StringUtils;
import com.lpwa.framework.netty.NettyServerHandler;
import com.lpwa.framework.netty.entity.LocatorMacCtx;
import com.lpwa.system.mapper.LocatorMapper;
import com.lpwa.system.mapper.SysLocatorMapper;
import com.lpwa.system.service.ILableService;
import com.lpwa.system.service.ILocatorService;
import com.lpwa.system.service.ITagDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author tanmu
 */
@Component("ryTask")
public class RyTask
{
//    @Autowired
//    private IJnaService jnaService;
    @Autowired
    private ITagDataService tagDataService;

    @Autowired
    private ILableService lableService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ILocatorService locatorService;
    @Autowired
    private SysLocatorMapper sysLocatorMapper;
    @Autowired
    private NettyServerHandler nettyServerHandler;

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }
    /**
     * 获取标签实时数据
     */
//    public void getTagData()
//    {
//        tagDataService.getTagData();
//    }
    /**
     * 通过Tdgs中间件接口获取标签数据信息并做相关处理
     * @return 结果
     */
    public void getTdgsTagData()
    {
        tagDataService.getTdgsTagData();
    }
    /**
     * 更新标签在线离线状态
     */
    public void updateIsOnline(String s)
    {
        lableService.updateIsOnline(Integer.parseInt(s));
    }
    /**
     * 更新redis中统计标签历史记录总数
     */
    public void  updateRedisTagDataCount() {
        Integer count = tagDataService.getCount();
        redisUtil.set("countTagData",count);
    }
    /**
     * 更新基站在线离线状态
     */
    public void updateLocatorIsOnline()
    {
        locatorService.updateLocatorIsOnline();
    }
    /**
     * 定时扫描基站下的设备并发送指令
     */
    public void scanSlave()
    {
        nettyServerHandler.scanSelve();
    }
    /**
     * 基站离线判断
     */
    public void locatorOnline()
    {
        String locatorMacs = StaticRedisUtil.staticRedisUtil.get("locatorMacs")==null?"":StaticRedisUtil.staticRedisUtil.get("locatorMacs").toString();
        if (!StringUtils.isEmpty(locatorMacs)){
            List<LocatorMacCtx> list = new ArrayList<>();
            list = JSONObject.parseArray(locatorMacs, LocatorMacCtx.class);
            //list为在线的基站，剩下的都是不在线的基站
            List<String> locatorList = new ArrayList<>();
            String [] str = new String[list.size()];
            for (int i = 0 ; i<list.size();i++){
                str[i] = list.get(0).getMac();
            }
            sysLocatorMapper.updateSysLocatorIsOnline(str);
            sysLocatorMapper.updateSysLocatorIsOffline(str);
            StaticRedisUtil.set("locatorMacs", JSON.toJSONString(list));
        }
    }
}
